<?php

namespace app\modules\Product\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\modules\Product\models\Product;

/**
 * ProductSearch represents the model behind the search form of `app\modules\Product\models\Product`.
 */
class ProductSearch extends Product
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'customer_id', 'num', 'status', 'user_id', 'currency'], 'integer'],
            [['name', 'code', 'p_name', 'p_phone', 'p_mail', 'p_department', 'city', 'begin_date', 'end_date', 'colse_date', 'oper_id', 'remark', 'created_at', 'updated_at'], 'safe'],
            [['price', 'cost_price'], 'number'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Product::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'customer_id' => $this->customer_id,
            'begin_date' => $this->begin_date,
            'end_date' => $this->end_date,
            'num' => $this->num,
            'colse_date' => $this->colse_date,
            'status' => $this->status,
            'currency' => $this->currency,
            'price' => $this->price,
            'cost_price' => $this->cost_price,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        if(!Yii::$app->user->can('all_product')){

            if(\yii::$app->user->identity->manage_user_id){
                $user_id = json_decode(\yii::$app->user->identity->manage_user_id);
                array_push($user_id,\yii::$app->user->id);
                $query->andFilterWhere(['user_id'=>$user_id]);
            }else{

                $query->andFilterWhere(['user_id'=>\yii::$app->user->id]);
            }
            
        }
        
        

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'p_name', $this->p_name])
            ->andFilterWhere(['like', 'p_phone', $this->p_phone])
            ->andFilterWhere(['like', 'p_mail', $this->p_mail])
            ->andFilterWhere(['like', 'p_department', $this->p_department])
            ->andFilterWhere(['like', 'city', $this->city])
            ->andFilterWhere(['like', 'oper_id', $this->oper_id])
            ->andFilterWhere(['like', 'remark', $this->remark]);

        return $dataProvider;
    }

    //操作ID
    public function search_oper($params)
    {
        $query = Product::find();

        // add conditions that should always apply here

        // $dataProvider = new ActiveDataProvider([
        //     'query' => $query,
        // ]);

        $this->load($params);

        // if (!$this->validate()) {
        //     // uncomment the following line if you do not want to return any records when validation fails
        //     // $query->where('0=1');
        //     return $dataProvider;
        // }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'customer_id' => $this->customer_id,
            'begin_date' => $this->begin_date,
            'end_date' => $this->end_date,
            'num' => $this->num,
            'colse_date' => $this->colse_date,
            'status' => $this->status,
            'currency' => $this->currency,
            'price' => $this->price,
            'cost_price' => $this->cost_price,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'p_name', $this->p_name])
            ->andFilterWhere(['like', 'p_phone', $this->p_phone])
            ->andFilterWhere(['like', 'p_mail', $this->p_mail])
            ->andFilterWhere(['like', 'p_department', $this->p_department])
            ->andFilterWhere(['like', 'city', $this->city])
            ->andFilterWhere(['like', 'oper_id', $this->oper_id])
            ->andFilterWhere(['like', 'remark', $this->remark]);

        $dataProvider = $query->all();

        return $dataProvider;
    }


}
